<?php
/**
 * Auther: Joshua Conero
 * Date: 2018/2/2 0002 23:48
 * Email: brximl@163.com
 * Name:
 */

namespace App\Http\Controllers\Api;


use hyang\Net;
use Illuminate\Session\Store;

class Login extends ApiBaseCtrl
{
    // 站内用户登录
    public function UserAction(){
        $request = request();
        $input = $request->all();
        $pigeons = $this->getPigeons();
        $data = [
            'access_token' => $pigeons->access_token,
            'project'       => $pigeons->code
        ];
        if(isset($input['pswd']) && isset($input['user_code'])){
            $data['pswd'] = $input['pswd'];
            $data['user_code'] = $input['user_code'];
            $header = [
                'Conero-Code' => $pigeons->code,
                'Conero-Pid'  => $pigeons->conero_pid
            ];
            $options = [
                'method'    => 'post',
                'data'      => $data,
                'header'    => $header
            ];
            $retVal = $pigeons->curl('aurora/user/login', $options);
            if($retVal && isset($retVal['code']) && 200 == $retVal['code']){
                $token = $retVal['token'];

                $openId = $retVal['openid'];
                // 获取用户信息
                $options = [
                    'data' => [
                        'token' => $token
                    ],
                    'header' => $header
                ];

                $userInfo = $pigeons->curl('aurora/user/info', $options);
                if(!is_array($userInfo)){
                    $userInfo = [];
                }
                $userInfo['token'] = $token;
                $userInfo['openid'] = $openId;
                if(isset($userInfo['account'])){    // 兼容历史
                    $userInfo['user'] = $userInfo['account'];
                }
                // 保存 session
                $skey = $this->aurora('php_session.user_key');
                session([$skey=>$userInfo]);  // 仅在当前的ajax请求有效
                session()->save();

                $this->success('用户登录成功', [
                    'token' => $token,
                    'openid' => $retVal['openid'],
                ]);
            }else{
                $this->error('用户登录失败，可能是密码或者账户无效！');
            }
        }else{
            $this->error('请求参数不足而造成用户登录失败！');
        }
    }
    // 根据返回的 token 值保存当前的用户信息： 参数 token
    public function TokenToSession(){
        // session 检测
        $skey = $this->aurora('php_session.user_key');
        $userInfo = session($skey);
        if(!empty($userInfo) && is_array($userInfo) && isset($userInfo['user'])){
            //redirect('/');
            Net::go('/');
        }
        session([$skey=>[]]);
        // 处理请求
        $request = request();
        $input = $request->all();
        if(!$input || !isset($input['token'])){
            $this->error('请求参数不足，请传入token');
        }else{
            $token = $input['token'];
            $pigeons = $this->getPigeons();
            $header = [
                'Conero-Code' => $pigeons->code,
                'Conero-Pid'  => $pigeons->conero_pid
            ];
            // 获取用户信息
            $options = [
                'data' => [
                    'token' => $token
                ],
                'header' => $header
            ];
            $userInfo = $pigeons->curl('aurora/user/info', $options);
            //println($userInfo);die();
            if(empty($userInfo) || !is_array($userInfo) || !isset($userInfo['code']) || 400 == intval($userInfo['code'])){
                $this->error('token值无效，数据操作失败！');
            }
            unset($userInfo['code']);
            $userInfo['token'] = $token;
            if(isset($userInfo['account'])){    // 兼容历史
                $userInfo['user'] = $userInfo['account'];
            }
            // 保存 session
            $skey = $this->aurora('php_session.user_key');
            debug([$skey=>$userInfo], session($skey));
            session([$skey=>$userInfo]);  // 仅在当前的ajax请求有效
            session()->save();
//            println($skey, session($skey));die();
            //redirect('');
            Net::go('/');
            $this->success('登录成功');
        }
    }
    // 注销登录
    public function userQuit(){
        $skey = $this->aurora('php_session.user_key');
        session([$skey=>null]);
        session()->save();
        Net::go('/');
    }
}